//# sourceURL=http://uMapApp/UI/SplineDrawer
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth"
], function (
    mhjs,
    uMap,
    uEarth
) {

    function SplineDrawer(option) {

        var _this = this;

        _this.app = option.app;

        var viewer = _this.app.earth;
        var scene = _this.app.earth.scene;
        var canvas = _this.app.earth.canvas;

        _this.splineDrawer = new uMap.SplineDrawer({
            earth: viewer
        });

        //按钮动作

        _this.buttons = $("<div class='SplineDrawerButtons'></div>");

        var sampleSizeInput = $("<div class='input'><div class='label'>绘制精度（米）</div><input type='text' placeholder='点位间距' value='1'></div>");
        var interpolationAlgorithmSelect = $("" +
            "<div class='input'>" +
            "<div class='label'>插值类型</div>" +
            "<select>" +
            "<option>LagrangePolynomialApproximation</option>" +
            "<option>LinearApproximation</option>" +
            "<option>HermitePolynomialApproximation</option>" +
            "</select>" +
            "</div>");
        var interpolationDegreeInput = $("<div class='input'><div class='label'>插值阈值</div><input type='text' placeholder='插值阈值' value='2'></div>");
        var resultSizeInput = $("<div class='input'><div class='label'>采样距离（米）</div><input type='text' placeholder='采样距离' value='100'></div>");

        _this.buttons.startButton = $("<div class='button startButton'>开始/重新绘制</div>");
        _this.buttons.saveButton = $("<div class='button'>保存结果</div>");

        _this.buttons
            .append(sampleSizeInput)
            .append(interpolationAlgorithmSelect)
            .append(interpolationDegreeInput)
            .append(resultSizeInput)
            .append(_this.buttons.startButton)
            .append(_this.buttons.saveButton.hide())
        ;

        _this.buttons.startButton.click(function () {

            if (
                sampleSizeInput.find("input").val()
                && interpolationAlgorithmSelect.find("select").val()
                && interpolationDegreeInput.find("input").val()
                && resultSizeInput.find("input").val()
            ) {
                _this.splineDrawer.sampleDistance = Number(sampleSizeInput.find("input").val());
                _this.splineDrawer.resultDistance = Number(resultSizeInput.find("input").val());
                _this.splineDrawer.interpolationDegree = Number(interpolationDegreeInput.find("input").val());
                _this.splineDrawer.interpolationAlgorithm = uEarth[interpolationAlgorithmSelect.find("select").val()];
                _this.splineDrawer.start();
                _this.buttons.saveButton.hide();
            }

        });

        _this.splineDrawer.on("finish", function (drawer) {
            _this.buttons.saveButton.show();
        });

        _this.buttons.saveButton.click(function () {

            var resultFeature = {
                "type"       : "LineString",
                "coordinates": []
            };


            $.each(_this.splineDrawer.resultPositionEntities, function (i, resultPositionEntity) {
                resultFeature.coordinates.push(formatPosition(resultPositionEntity.position._value));
            });

            resultFeature.coordinates.push(formatPosition(_this.splineDrawer.drawPositionEntities[_this.splineDrawer.drawPositionEntities.length - 1].position._value));

            mhjs.SaveFile(new mhjs.GeoJSON([resultFeature]).toString(), "spline.geojson");


        });

        function formatPosition(position) {
            var cartographicPosition = uEarth.Cartographic.fromCartesian(position);
            return [uEarth.Math.toDegrees(cartographicPosition.longitude), uEarth.Math.toDegrees(cartographicPosition.latitude), cartographicPosition.height];
        }

        _this.buttons.hide();

    }

    var _class = SplineDrawer;

    Object.defineProperties(_class.prototype, {
        "open"  : {
            value: function () {

                var _this = this;

                if (!_this.isOpened) {

                    _this.buttons.show();
                    _this.isOpened = true;

                }

            }
        },
        "close" : {
            value: function () {

                var _this = this;

                if (_this.isOpened) {

                    _this.buttons.hide();
                    _this.splineDrawer.reset();
                    _this.buttons.saveButton.hide();
                    _this.isOpened = false;

                }

            }
        },
        "toggle": {
            value: function () {
                var _this = this;
                if (_this.isOpened) {
                    _this.close();
                } else {
                    _this.open();
                }
            }
        }
    });

    return _class;

});